/*
 * Copyright © 2024-2025 Wenze Wei
 *
 * This file is part of StadionOS.
 *
 * This Source Code Form is subject to the terms of the GNU General Public License, v. 2.0.
 * You can obtain a copy of the GPL at
 * https://www.gnu.org/licenses/old-licenses/gpl-2.0.html.
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
 * the specific language governing rights and limitations under the License.
 */

#include "../include/fsd.h"

/**
 * @brief Convert mode string to file open flags.
 *
 * @param mode The mode string (e.g., "r", "w", "a").
 * @return int The corresponding open flags, or -1 on error.
 */
int Sflags(const char *mode) {
    if (mode == NULL)
        return -1;

    if (Sstrcmp(mode, "r") == 0) {
        return DO_RDONLY;
    }
    else if (Sstrcmp(mode, "w") == 0) {
        return DO_WRONLY | DO_CREAT | DO_TRUNC;
    }
    else if (Sstrcmp(mode, "a") == 0) {
        return DO_WRONLY | DO_CREAT | DO_APPEND;
    }
    else if (Sstrcmp(mode, "r+") == 0) {
        return DO_RDWR;
    }
    else if (Sstrcmp(mode, "w+") == 0) {
        return DO_RDWR | DO_CREAT | DO_TRUNC;
    }
    else if (Sstrcmp(mode, "a+") == 0) {
        return DO_RDWR | DO_CREAT | DO_APPEND;
    }

    return -1;
}
